package com.fortune.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MUserMgtExcution;
import com.fortune.dto.in.mUserMgt.MUserAddDto;
import com.fortune.dto.in.mUserMgt.MUserEditDto;
import com.fortune.dto.in.mUserMgt.MUserListDto;
import com.fortune.dto.in.mUserMgt.MUserLogableDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MUserMgtEnum;
import com.fortune.service.MUserMgtService;
import com.fortune.vo.mUserMgt.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@Api(value = "muser", description="厂商员工管理", tags = {"厂商员工管理"})
@RestController
@RequestMapping("mUserMgt")
@Validated
public class MUserMgtController{
	@Autowired
	private MUserMgtService mUserMgtService;
	@ApiOperation(value = "厂商员工列表", nickname = "mUserList", notes = "@岳希伟")
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
	})
	@GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<User>> list(HttpServletRequest request,@Validated @ApiIgnore MUserListDto listDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<User> pagingData = mUserMgtService.pagingData(mUserInfo, listDto);
		return new MUserMgtExcution<PagingData<User>>(pagingData, MUserMgtEnum.SUCCESS);
	}
	@ApiOperation(value = "添加厂商员工", nickname = "mUserAdd", notes = "@岳希伟")
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody MUserAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mUserMgtService.addMUser(mUserInfo, addDto);
		if (result==2) {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.PHONEFALSE);
		}else if (result==3) {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.NUMBERFALSE);
		}else if (result==1) {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.SUCCESS);
		} else {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.FAILSE);
		}
	}
	
	@ApiOperation(value = "编辑厂商员工", nickname = "mUserEdit", notes = "@岳希伟")
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody MUserEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mUserMgtService.edit(mUserInfo, editDto);
		if (result==2) {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.CHECKUSER);
		}else if (result==3) {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.CHECKPHONE);
		}else if (result==1) {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.SUCCESS);
		} else {
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.FAILSE);
		}
	}
	
	@ApiOperation(value = "删除厂商员工", nickname = "mUserDelete", notes = "@岳希伟")
	@ApiImplicitParam(name="userId", value="用户id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String userId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mUserMgtService.delete(mUserInfo, userId);
		if(result==0)
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.FAILSE);
		return new MUserMgtExcution<Void>(null, MUserMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "重置厂商员工密码", nickname = "mUserResetPassword", notes = "@岳希伟")
	@ApiImplicitParam(name="userId", value="用户id",paramType="query",dataType="string")
    @PutMapping(value = "resetPassword",
            produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String userId) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mUserMgtService.resetPassword(mUserInfo, userId);
		if(result==0)
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.FAILSE);
		return new MUserMgtExcution<Void>(null, MUserMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "设置厂商员工是否允许登录", nickname = "mUserLogableSetting", notes = "@岳希伟")
    @PutMapping(value = "logableSetting",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> logableSetting(HttpServletRequest request,@Validated @RequestBody MUserLogableDto logableDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = mUserMgtService.setLogin(mUserInfo, logableDto);
		if(result==0)
			return new MUserMgtExcution<Void>(null, MUserMgtEnum.FAILSE);
		return new MUserMgtExcution<Void>(null, MUserMgtEnum.SUCCESS);
	}
}
